# -*- coding: utf-8 -*-
import logging
from odoo import models, fields, api, _

_logger = logging.getLogger(__name__)

class ResUsers(models.Model):
    """
    通过设置用户的上下级关系实现：
    1、用户只看自己的和下级的业务数据
    """
    _inherit = 'res.users'

    parent_id = fields.Many2one('res.users', string='Direct Leader', index=True, ondelete='cascade')
    child_ids = fields.One2many('res.users', 'parent_id', string='Subordinates', domain=[('active', '=', True)])


class Partner(models.Model):
    _inherit = "res.partner"

    child_ids = fields.One2many('res.partner', 'parent_id', string='Subordinates', domain=[('active', '=', True)])

    @api.model
    def create(self, vals):
        if vals.get("customer") and not vals.get("user_id"):
            vals["user_id"] = self.env.user.id
        return super(Partner, self).create(vals)

class CrmTeam(models.Model):
    """
    扩展销售团队对象，增加事业部总经理和业务总监属性。
    事业部总经理下辖多个业务总监，可以查看下属所有销售团队的业务数据，
    业务总监下辖多个销售团队，可以查看下属所有销售团队的业务数据
    """
    _inherit = 'crm.team'

    # 事业部总经理。事业部下辖多个事业分部，每个事业分部下辖多个销售团队
    division_manager_id = fields.Many2one('res.users', string="Business unit's manager")

    # 业务总监。位于事业部之下，管理多个销售团队的领导目岗位
    business_director_id = fields.Many2one('res.users', string="Business director of sale team")
